package com.bluean.daily.cloud.service.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: blue_an
 * @date: 2023/12/31/11:29
 * @description: TODO
 */
@RestController
@RequestMapping(value = "systemTest")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "daily-system-test")
public class SystemTestController {

    @GetMapping("/test")
    @ApiOperation(value = "system 测试接口")
    public String test() {
        return "系统功能测试接口";
    }

    @ApiOperation(value = "限流测试")
    @GetMapping(value = "sentinel/protected")
    public String sentinelProtected() {
        return "访问的是限流测试接口";
    }

    @ApiOperation(value = "慢调用比例熔断策略")
    @GetMapping(value = "sentinel/slow/request/ratio")
    public String sentinelRR() {
        try {
            double randomNumber;
            randomNumber = Math.random();
            if (randomNumber >= 0 && randomNumber <= 0.80) {
                Thread.sleep(300L);
            } else if (randomNumber >= 0.80 && randomNumber <= 0.80 + 0.10) {
                Thread.sleep(10L);
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "慢调用比例熔断策略";
    }

    @ApiOperation(value = "异常比例熔断策略")
    @GetMapping(value = "sentinel/error/ratio")
    public String sentinelRatio() {
        int i = 1/0;
        return "异常比例熔断策略";
    }

    @ApiOperation(value = "异常数熔断策略")
    @GetMapping(value = "sentinel/error/count")
    public String sentinelCount() {
        int i = 1/0;
        return "异常数熔断策略";
    }

}
